const router = require('koa-router')();

const User = require('../model/user')
const ArticleType = require('../model/articleType')
const Article = require('../model/article')

/**
 * 获取文章类型列表
 */
router.post('/type-lists', async (context, next) => {
    let typeLists = await ArticleType.find()
    typeLists = typeLists.map( item => {
        return {id:item._id, type: item.type}
    })
    context.body = {typeLists}
})

/**
 * 获取文章列表
 */
router.post('/article-lists', async (context, next) => {
    let body = context.request.body

    context.verifyParams({
        type:{
            type: 'string',
            required: true
        }
    })
    const articleLists = await Article.find({typeID: body.type})
    let lists = articleLists.map( item => {
        return {
            id: item._id,
            title: item.title,
            authName: item.authName,
            authID: item.authID,
            createdAt: item.createdAt
        }
    })
    context.body = {articleLists:lists}
})

/**
 * 获取文章内容
 */
router.post('/article', async (context, next) => {
    let body = context.request.body

    context.verifyParams({
        id:{
            type: 'string',
            required: true
        }
    })
    const id = body.id
    let article = await Article.findById(id)
    const {
        typeID,
        title,
        content,
        createdAt,
        authID,
        authName
    } = article

    const {type: typeName} = await ArticleType.findById(typeID)
    const con = {
        id,
        typeID,
        typeName,
        title,
        content,
        createdAt,
        authID,
        authName
    }
    context.body = {article: con}
})

/**
 * 发表文章
 */
router.post('/publish', async (context, next) => {
    let body = context.request.body

    context.verifyParams({
        token:{
            type: 'string',
            required: true
        },
        typeID: {
            type: 'string',
            required: true
        },
        title:{
            type: 'string',
            required: true
        },
        content:{
            type: 'string',
            required: true
        },
        createdAt:{
            type: 'string',
            required: true
        }
    })
    //  在此根据token查id与用户名        authID
    const auth = await User.find({token: body.token})
    const authID = auth[0]._id + ""
    const authName = auth[0].nickName
   const {
        typeID,
        title,
        content,
        createdAt
    } = body
    const newArticle = {
        typeID,
        authID,
        authName,
        title,
        content,
        createdAt
    }
    const checkData = {
        title,
        content
    }
    const check = await Article.find(checkData)
    if(check.length === 0){
        await new Article(newArticle).save()

        const articles = await Article.find(newArticle)
    
        const id = articles[0]._id
        body.id = id
        context.body = body
    }else {
        context.throw(412, "这篇文章好像发表过了")
    }
 
})
module.exports = router